package site.yunnong.atvris.service.impl;

import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import site.yunnong.atvris.common.enumeration.PartitionLevelEnum;
import site.yunnong.atvris.dao.domain.MediaPartition;
import site.yunnong.atvris.dao.domain.MediaPartitionExample;
import site.yunnong.atvris.dao.mapper.MediaPartitionMapper;
import site.yunnong.atvris.service.MediaPartitionService;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 分区表
 *
 * @author zhangjinghao
 * @email jhzhang9102@163.com
 * @date 2021-07-09 23:45:38
 */
@Service
public class MediaPartitionServiceImpl implements MediaPartitionService {

    @Resource
    private MediaPartitionMapper mediaPartitionMapper;
    
    @Override
    public List<MediaPartition> queryAimLevelPartition(Long parentId, PartitionLevelEnum level) {
        MediaPartitionExample example = new MediaPartitionExample();
        MediaPartitionExample.Criteria criteria = example.createCriteria();
        if (parentId != null) {
            criteria.andParentIdEqualTo(parentId);
        }
        if (level != null) {
            criteria.andPartitionLevelEqualTo(level.getValue());
        }
        return mediaPartitionMapper.selectByExample(example);
    }

    @Override
    public List<MediaPartition> queryAll() {
        MediaPartitionExample example = new MediaPartitionExample();
        return mediaPartitionMapper.selectByExample(example);
    }

    @Override
    public List<MediaPartition> querySinglePartition() {
        List<MediaPartition> mediaPartitionList = queryAll();
        return filterSinglePartition(mediaPartitionList);
    }

    @Override
    public List<Long> queryChildPidList(Long pid) {
        MediaPartitionExample example = new MediaPartitionExample();
        example.createCriteria().andParentIdEqualTo(pid);
        List<MediaPartition> secondMediaPartitionList = mediaPartitionMapper.selectByExample(example);
        List<Long> result = Lists.newArrayList();
        if (CollectionUtils.isEmpty(secondMediaPartitionList)) {
            // 无子分区，返回自己
            return Collections.singletonList(pid);
        } else {
            // 有子分区，查子子分区
            for (MediaPartition secondMediaPartition : secondMediaPartitionList) {
                MediaPartitionExample mediaPartitionExample = new MediaPartitionExample();
                mediaPartitionExample.createCriteria().andParentIdEqualTo(secondMediaPartition.getId());
                List<MediaPartition> thirdMediaPartitionList = mediaPartitionMapper.selectByExample(mediaPartitionExample);
                if (CollectionUtils.isEmpty(thirdMediaPartitionList)) {
                    // 无下级子分区
                    result.add(secondMediaPartition.getId());
                } else {
                    // 将下级子分区全部加入
                    thirdMediaPartitionList.forEach(thirdMediaPartition -> result.add(thirdMediaPartition.getId()));
                }
            }
        }
        return result;
    }

    private List<MediaPartition> filterSinglePartition(List<MediaPartition> mediaPartitionList) {
        if (CollectionUtils.isEmpty(mediaPartitionList)) {
            return Lists.newArrayList();
        }
        return mediaPartitionList.stream().filter(mediaPartition -> {
            // 如果没有匹配，返回该条数据，否则不返回
            return mediaPartitionList.stream().noneMatch(partition -> 
                    partition.getParentId().equals(mediaPartition.getId())
            ) && !mediaPartition.getParentId().equals(0L);
        }).collect(Collectors.toList());
    }
}

